home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Softshoe / Lisa's Portable Parts / Arrays / ConstArrayOf.cp < prev    next >
Encoding:
Text File  |  2000-06-23  |  2.4 KB  |  104 lines

  1. // ConstArrayOf.cp
  2. #define ConstArrayOf_cp
  3.  
  4. #ifndef ConstArrayOf_h
  5. #include "ConstArrayOf.h"
  6. #endif
  7. #ifndef MinMax_h
  8. #include "MinMax.h"
  9. #endif
  10.  
  11. template < class Element >
  12. void ConstArrayOf<Element>::Truncate( uint32 position )
  13.   {
  14.     Assert( !Null() );
  15.     Assert( position <= length )
  16.     length = position;
  17.   }
  18.  
  19. template < class Element >
  20. void ConstArrayOf<Element>::Append( ConstArrayType tail )
  21.   {
  22.     Assert( !Null() );
  23.     Assert( Precedes( tail ) );
  24.     length += tail.length;
  25.   }
  26.  
  27. template < class Element >
  28. const ConstArrayOf<Element> ConstArrayOf<Element>::operator+( ConstArrayType tail ) const
  29.   {
  30.     ConstArrayType result( *this );
  31.     result += tail;
  32.     return result;
  33.   }
  34.  
  35. template < class Element >
  36. void ConstArrayOf<Element>::Prepend( ConstArrayType head )
  37.   {
  38.     Assert( !Null() );
  39.     Assert( Follows( head ) );
  40.     start = head.start;
  41.     length += head.length;
  42.   }
  43.  
  44. template < class Element >
  45. bool ConstArrayOf<Element>::operator<=( ConstArrayType r ) const
  46.   {
  47.     return start >= r.start && End() <= r.End();
  48.   }
  49.  
  50. template < class Element >
  51. bool ConstArrayOf<Element>::operator<( ConstArrayType r ) const
  52.   {
  53.     return *this <= r && *this != r;
  54.   }
  55.  
  56. template < class Element >
  57. bool ConstArrayOf<Element>::IsHeadOf( ConstArrayType r ) const
  58.   {
  59.     return start == r.start && length <= r.length;
  60.   }
  61.  
  62. template < class Element >
  63. bool ConstArrayOf<Element>::IsTailOf( ConstArrayType r ) const
  64.   {
  65.     return End() == r.End() && length <= r.length;
  66.   }
  67.  
  68. template < class Element >
  69. bool ConstArrayOf<Element>::Touches( ConstArrayType r ) const
  70.   {
  71.     return End() >= r.start && start <= r.End();
  72.   }
  73.  
  74. template < class Element >
  75. bool ConstArrayOf<Element>::IsSortedBy( Comparison compare ) const
  76.   {
  77.     for ( uint32 i = 0; i+1 < length; i++ )
  78.         if ( compare( start[i], start[i+1] ) > 0 )
  79.             return false;
  80.     return true;
  81.   }
  82.  
  83. template < class Element >
  84. const ConstArrayOf<Element> ConstArrayOf<Element>::operator&( ConstArrayType r ) const
  85.   {
  86.     Assert( !Null() );
  87.     Assert( !r.Null() );
  88.     Assert( Touches( r ) );
  89.     const Element *lastStart = Max( start, r.start );
  90.     const Element *firstEnd = Min( End(), r.End() );
  91.     return ConstArrayType( lastStart, firstEnd - lastStart );
  92.   }
  93.  
  94. template < class Element >
  95. const ConstArrayOf<Element> ConstArrayOf<Element>::operator|( ConstArrayType r ) const
  96.   {
  97.     Assert( !Null() );
  98.     Assert( !r.Null() );
  99.     Assert( Touches( r ) );
  100.     const Element *firstStart = Min( start, r.start );
  101.     const Element *lastEnd = Max( End(), r.End() );
  102.     return ConstArrayType( firstStart, lastEnd - firstStart );
  103.   }
  104.